<!-- 展示logo和登录用户信息 -->
<script lang="ts" setup>
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import useStore from '../store/modules/User';
import { userLogout } from '../services/user';
import ROUTER_CONSTANTS from '../route/constants';

// User store
const userStore = useStore();
// 全局路由
const router = useRouter();

// 用户名下拉菜单选择事件
const handleCommand = async (command: string) => {
  if (command === 'loginout') {
    try {
      const response = await userLogout();
      if (response.status === 200) {
        userStore.logout();
        router.replace({
          name: ROUTER_CONSTANTS.LOGIN,
        });
      }
    } catch (error) {
      ElMessage.error('登出失败');
    }
  }
};
</script>
<template>
  <div class="header">
    <div class="logo">PeekpaJob 管理系统</div>
    <div class="header-right">
      <div class="header-user-con">
        <el-avatar fit="fill" shape="square" :src="userStore.getAvatar">
        </el-avatar>
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            {{ userStore.getName }}
            <el-icon><eli-CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="loginout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<style scoped>
.header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 70px;
  font-size: 22px;
  color: #fff;
  background-color: #242f42;
}
.header .logo {
  float: left;
  width: 250px;
  line-height: 70px;
  font-family: HyliaSerif;
  margin-left: 50px;
}
.header-right {
  float: right;
  padding-right: 50px;
}
.header-user-con {
  display: flex;
  height: 70px;
  align-items: center;
}
.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}

.user-name {
  margin-left: 10px;
}
.user-avator {
  margin-left: 20px;
}
.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}
.el-dropdown-menu__item {
  text-align: center;
}
</style>
